<?php
class Deal extends MX_Controller {
	function __construct()
	{
		parent::__construct();
		$this->load->model('deal_model');
		$this->lang->load('error', $this->public_model->getLanguage());
	}
	function all_deals($categories_id = null, $ajax = null, $recent_deal = null)
    {
        
        //get paging
        $page = $this->input->post("page");
		if(empty($page)) $page = 1;
		$itemsPerPage = $this->input->post("item_per_page");
		if(empty($itemsPerPage)) $itemsPerPage = $this->public_model->getSystemParameterValue("itemsPerPage");
		$numberPageDisplay = $this->public_model->getSystemParameterValue("numberPageDisplay");
        $data = array();
        $data['category_id'] = $categories_id;
        $data['desc'] = $recent_deal;
        $city_id = $this->session->userdata('city_session');
        $options = array(
            'city_id' => $city_id,
            "limit" => $itemsPerPage,
            "offset" => ($page - 1) * $itemsPerPage
        );
        if($recent_deal != null && $recent_deal)
            $options['desc'] = 'id';
        
        if ($categories_id != null && $categories_id!=-1)
            $options['category_id'] = $categories_id;
        $deals = $this->public_model->getDeal($options);
        $total = $this->public_model->getCountDeal($options);
        
        if ($categories_id == null || $categories_id == -1 )
        {
            $data['category_name'] = ALL_DEALS;
            for($i = 0; $i < count($deals); $i++)
            {
                $options = array(
                    'category_id' => $deals[$i]->id_categories,
                    'enable' => ENABLED,
                );
                $categories = $this->public_model->getCategory($options);
                
                if ($categories != null)
                    $deals[$i]->view = $categories->view;
                else
                    $deals[$i]->view = -1; //in case category disabled
            }
        }
        else
        {
            $options = array(
                'category_id' => $categories_id,
                'enable' => ENABLED,
            );
            $categories = $this->public_model->getCategory($options);
            if ($categories != null)
                $data['category_name'] = "<a href='/'>".ALL_DEALS."</a> &raquo; " . $categories->name ;
            for($i = 0; $i < count($deals); $i++){
                if ($categories != null)
                    $deals[$i]->view = $categories->view; 
                else
                    $deals[$i]->view = -1;
            }
        }
            
        $data['deals'] = $deals;
        
        
        
        $pPaging = $this->commonutils->paging($page, $itemsPerPage, $total, $numberPageDisplay);
		$pPaging['pag_function'] = "ajax_get_more_deals";
		$pPaging['pag_item_name'] = "product(s)";
		$pPaging['itemsPerPage'] = $itemsPerPage;
		$data['pPaging'] = $pPaging;
        
        if ($ajax != null)
            $this->load->view('deal_view', $data);
        else
            $this->load->view('list_view', $data);
    }
    
    function detail($deal_id=null)
    {
        if ($deal_id == null)
        {
            echo Modules::run('common/error_404');
        }
        else
        {
            $options = array(
                'id' => $deal_id
            );
            $deal = $this->public_model->getDeal($options);
            if (count($deal) == 0)
            {
                echo Modules::run('common/error_404');    
            }   
            else
            {
                $data['deal'] = $deal[0];
                
                $data['categories'] = $this->public_model->getCategories(array('enable'=>ENABLED));
                $content = $this->load->view('detail_view', $data, true);
                Template::display($content);
            }
                
        }
    }
    
	
}